package com.expert.controller;


import com.expert.common.jqgrid.GridResult;
import com.expert.common.page.MyPage;
import com.expert.common.utils.FormUtils;
import com.expert.entity.Feature;
import com.expert.entity.Result;
import com.expert.service.FeatureService;
import com.expert.service.ResultService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.expert.common.base.BaseController;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.expert.service.impl.RuleServiceImpl;
import com.expert.entity.Rule;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author Mankind
* @since 2018-06-03
*/
@Controller
@RequestMapping("/console/rule")
public class RuleController extends BaseController {
    @Autowired
    private RuleServiceImpl ruleService;
    @Autowired
    private FeatureService featureService;
    @Autowired
    private ResultService resultService;
    @ModelAttribute("rule")
    public Rule get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return ruleService.selectById(id);
        }else{
            return new Rule();
        }
    }

    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        return "/admin/rule/list";
    }

    @RequestMapping(value="/forecast",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String forecast(Model model) throws Exception{
        model.addAttribute("features",featureService.selectList(null));
        return "/admin/rule/forecast";
    }


    @ResponseBody
    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    public GridResult list() throws Exception{
        MyPage<Rule> page = getPage();
        page = ruleService.list(page);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑界面
     * @param rule
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute Rule rule,Model model) throws Exception{
        model.addAttribute("features",featureService.selectList(null));
        if((rule.getEndResult()!=null&&rule.getEndResult()==true)||rule.getEndResult()==null){
            model.addAttribute("results",resultService.selectList(null));
        }else{
            model.addAttribute("results",featureService.selectList(null));
        }
        if(StringUtils.isNotEmpty(rule.getFacts())){
            Long[] ids= FormUtils.getIdsArray(rule.getFacts());
            model.addAttribute("allocatedList",featureService.selectBatchIds(Arrays.asList(ids)));
        }
        model.addAttribute("rule",rule);
        return "/admin/rule/edit";
    }

    /**
     * 处理更新的请求
     * @param rule
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Rule rule){
        Map<String,Object> result = new HashedMap();
        try {
        if(rule.getId() != null && rule.getId() > 0){
            ruleService.updateById(rule);
        }else {
            ruleService.insert(rule);
        }
        result.put("status",200);
        result.put("msg","rule信息保存成功");
        }catch (Exception e){
        e.printStackTrace();
        result.put("status",500);
        result.put("msg","rule信息保存失败");
        }
        return result;
    }

    /**
     * 处理删除的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashedMap();
        try {
        if(id != null && id > 0){
            ruleService.deleteById(id);
            result.put("status",200);
            result.put("msg","删除规则成功");
        }else {
            result.put("status",500);
            result.put("msg","请选择删除的规则");
        }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除规则失败");
        }
        return result;
    }

    @RequestMapping(value = "/getResultList", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getResultList(Boolean endResult){
        Map<String,Object> result = new HashedMap();
        if(endResult==true){
            result.put("data",resultService.selectList(null));
        }else{
            result.put("data",featureService.selectList(null));
        }
        return result;
    }


    @RequestMapping(value = "/forecast", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> forecast(String facts){
        Map<String,Object> result = new HashedMap();
        if(StringUtils.isEmpty(facts)){
            result.put("status",500);
            result.put("msg","请至少选择一个特征");
        }else{
            Result answer=ruleService.forecast(facts);
            if(answer==null){
                result.put("status",200);
                result.put("msg","预测无结果");
            }else{
                result.put("status",200);
                result.put("msg","预测出动物【"+answer.getName()+"】");

            }
        }
        return result;
    }


}
